import { validUsername } from '@/utils/validate';

export default (await import('vue')).defineComponent({
name: 'Login',
components: { SocialSign },
data() {
const validateUsername = (rule, value, callback) => {
if (!validUsername(value)) {
callback(new Error('Please enter the correct user name'));
} else {
callback();
}
};
const validatePassword = (rule, value, callback) => {
if (value.length < 6) {
callback(new Error('The password can not be less than 6 digits'));
} else {
callback();
}
};
return {
loginForm: {
username: 'admin',
password: '111111'
},
loginRules: {
username: [{ required: true, trigger: 'blur', validator: validateUsername }],
password: [{ required: true, trigger: 'blur', validator: validatePassword }]
},
passwordType: 'password',
capsTooltip: false,
loading: false,
showDialog: false,
redirect: undefined,
otherQuery: {}
};
},
watch: {
$route: {
handler: function (route) {
const query = route.query;
if (query) {
this.redirect = query.redirect;
this.otherQuery = this.getOtherQuery(query);
}
},
immediate: true
}
},
created() {
// window.addEventListener('storage', this.afterQRScan)
},
mounted() {
if (this.loginForm.username === '') {
this.$refs.username.focus();
} else if (this.loginForm.password === '') {
this.$refs.password.focus();
}
},
destroyed() {
// window.removeEventListener('storage', this.afterQRScan)
},
methods: {
checkCapslock(e) {
const { key } = e;
this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z');
},
showPwd() {
if (this.passwordType === 'password') {
this.passwordType = '';
} else {
this.passwordType = 'password';
}
this.$nextTick(() => {
this.$refs.password.focus();
});
},
handleLogin() {
this.$refs.loginForm.validate(valid => {
if (valid) {
this.loading = true;
this.$store.dispatch('user/login', this.loginForm)
.then(() => {
this.$router.push({ path: this.redirect || '/', query: this.otherQuery });
this.loading = false;
})
.catch(() => {
this.loading = false;
});
} else {
console.log('error submit!!');
return false;
}
});
},
getOtherQuery(query) {
return Object.keys(query).reduce((acc, cur) => {
if (cur !== 'redirect') {
acc[cur] = query[cur];
}
return acc;
}, {});
}
// afterQRScan() {
//   if (e.key === 'x-admin-oauth-code') {
//     const code = getQueryObject(e.newValue)
//     const codeMap = {
//       wechat: 'code',
//       tencent: 'code'
//     }
//     const type = codeMap[this.auth_type]
//     const codeName = code[type]
//     if (codeName) {
//       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
//         this.$router.push({ path: this.redirect || '/' })
//       })
//     } else {
//       alert('第三方登录失败')
//     }
//   }
// }
}
});
